package cn.dubito.learning.security.core.validator.config;

import cn.dubito.learning.security.core.properties.SecurityProperties;
import cn.dubito.learning.security.core.validator.code.ImageCodeGenerator;
import cn.dubito.learning.security.core.validator.code.ValidateCodeGenerator;
import cn.dubito.learning.security.core.validator.code.sms.DefaultSmsCodeSender;
import cn.dubito.learning.security.core.validator.code.sms.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liliang
 * @description:
 * @date 2018/5/7.
 */
@Configuration
public class ValidateCodeBeanConfig {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 系统会先找 imageCodeGenerator 这个 bean 的实现 如果找不到 就用这个方法里的实现
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "imageCodeGenerator")
    public ValidateCodeGenerator imageCodeGenerator() {
        ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
        codeGenerator.setSecurityProperties(securityProperties);
        return codeGenerator;
    }

    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    public SmsCodeSender smsCodeSender() {

        return new DefaultSmsCodeSender();
    }
}
